// pages/itemDetail/itemDetail.js
const app = getApp()
const cfg = require("../../utils/config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标  1表示显示  0表示不显示
      title: '项目详情', //导航栏 中间的标题
      white: true, // 是就显示白的，不是就显示黑的。
      address: '' // 加个背景 不加就是没有
    },
    height: app.globalData.height * 2 + 30,
    checkIcon: '../../images/Check3x.png',
    showModalStatus: false,
    tempFileUp: '',
    tempFileDwon: '',
    bankName: '',
    agentBankAccount: '',
    bankCode: '',
    bankNameSubject: '',
    agentBankName: '',
    bankList: [],
    personList:[],
    checkTrue: "",
    items: [
      { name: 'USA', value: '同意自由职业者协议' },
    ],
    projectId:'',
    personInfoId:'',
    projectReceiveStatus:1,
    d_url : cfg.requestURL
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.personInfoId);
    this.setData({
      projectId: options.projectId,
      personInfoId: options.personInfoId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBankList();
    if (wx.getStorageSync('token') != "") {
      this.getPersonInfoId();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }, 
  getPersonInfoId: function () {
    var _this = this;
    wx.request({
      url: cfg.requestURL + '/backend/person/mobile/getPersonByloginId?token=' + wx.getStorageSync('token') +  '&loginId=' + wx.getStorageSync('userId'), //仅为示例，并非真实的接口地址
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.hideLoading();
        if (res.data.flag) {
          console.log("========1111" + res.data.data.personId)
          _this.setData({
            personList: res.data.data,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true,
            duration: 1500
          })
        }
      }
    })
  },
  getBankList: function () {
    var _this = this;
    wx.request({
      url: cfg.requestURL + '/backend/seller/mobile/project/view?token=' + wx.getStorageSync('token')
      + '&personInfoId='+wx.getStorageSync('personId') + '&projectId=' + _this.data.projectId, //仅为示例，并非真实的接口地址
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.flag) {
          _this.setData({
            bankList: res.data.data
          })
          _this.setData({
            projectReceiveStatus: res.data.data.havePersonInfo,
            projectStatus: res.data.data.projectStatus
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true,
            duration: 1500
          })
        }
      }
    })
  },
  getcurPagesUrl: function(options){
    var _url = this.route,
        _op = this.options,
        url = _url +'?projectId='+ _op.projectId+'&sellerCompanyName='+_op.sellerCompanyName+'&personInfoId='+_op.personInfoId;
    return url;
  },
  addBank: function () {
    var _this = this;

    if (wx.getStorageSync('token') == "" || (wx.getStorageSync('authorityName') == 'SELLER_USER')) {

  

      // 未登录用户存储信息，
      var _curl = _this.getcurPagesUrl();
      var _arr = [_curl,'itemDetail'];//[this.route,''];

      wx.setStorageSync("noLoginInfoSv", _arr);

      var tipsTxt = '';
      if((wx.getStorageSync('authorityName') == 'SELLER_USER')){
        tipsTxt = '当前登录为企业用户，将为您跳转到登录页面，登录个人用户再承接项目！';
      }else{
        tipsTxt = '您还没有登录，将为您跳转到登录页面';
      }
      wx.showModal({
        title: "温馨提示",
        content: tipsTxt,
        confirmText: "确定",
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../login/login'
            })
          }
        }
      });
      return false;
    } else {
      wx.showLoading({
        title: '加载中...',
      })
    } 
    console.log("personList+" + _this.data.personList.personId)
    wx.request({
      url: cfg.requestURL + '/backend/seller/mobile/insertProjectPerson?token=' + wx.getStorageSync('token'), //仅为示例，并非真实的接口地址
      data: {
        "sellerId": _this.data.bankList.sellerId,
        "personInfoId": _this.data.personList.personId,
        "projectId": _this.data.bankList.projectId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.hideLoading();
        if (res.data.flag) {
          _this.setData({
            personInfoId:'已承接',
            projectReceiveStatus:2
          })
          wx.showToast({
            title: "项目承接成功",
            icon: 'success',
            mask: true,
            duration: 1500
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            mask: true,
            duration: 1500
          })
        }
      }
    })
  },
  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  changeBank: function (event) {
    let postId = event.currentTarget.dataset.id;
    let name = event.currentTarget.dataset.name;
    this.setData({
      bankCode: postId,
      bankName: name
    })
    this.hideModal();
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
  }
})